package za.co.pas.spaceandtime.model;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import za.co.pas.business.abstracts.AbstractDataModel;
import za.co.pas.business.data.Company;
import za.co.pas.business.message.StatusMessage;
import za.co.pas.business.message.StatusMessageObject;
import za.co.pas.spaceandtime.data.Building;

public class BuildingDataModel extends AbstractDataModel
{

    private static Logger logger = Logger.getLogger(BuildingDataModel.class);
    private List<Building> buildings = new LinkedList<Building>();
//    private List<RoomType> roomTypes = new LinkedList<RoomType>();
//    private List<BedType> bedTypes = new LinkedList<BedType>();

    /**
     * @return the buildings
     */
    public List<Building> getBuildings(Company c, boolean force)
    {
        if ((force) || (buildings == null) || (buildings.isEmpty()) || (!buildings.get(0).getCompany().check(c)))
        {
            buildings = getBuildingsDB(c);
            Collections.sort((List) buildings);
        }
        return buildings;
    }

    private List<Building> getBuildingsDB(Company c)
    {
        try
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_INFO, "Getting all buildings", StatusMessageObject.SMO_TYPE_READ));
            Query q = em.createQuery("SELECT b FROM Building b WHERE b.company = :cmpy").setParameter("cmpy", c);
            setConnectionOk(true);
            return (List<Building>) q.getResultList();
        }
        catch (NoResultException ex)
        {
            //This is not a error
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_WARN, "No results for buildings", StatusMessageObject.SMO_TYPE_READ));
            logger.info(ex.getMessage());
        }
        catch (NonUniqueResultException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalStateException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        catch (IllegalArgumentException ex)
        {
            firePropertyChange(new StatusMessage(StatusMessageObject.SMO_ERROR, "Reading error: " + ex.getMessage(), StatusMessageObject.SMO_TYPE_READ));
            logger.error(ex.getMessage(), ex);
        }
        return null;
    }

    /**
     * @param buildings the buildings to set
     */
    public void setBuildings(List<Building> buildings)
    {
        this.buildings = buildings;
    }

    
}
